package wiki.zimo.certificate_manage.controller;

import cn.afterturn.easypoi.excel.entity.ExportParams;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import wiki.zimo.certificate_manage.constant.Constant;
import wiki.zimo.certificate_manage.dao.MajorDao;
import wiki.zimo.certificate_manage.dao.SchoolDao;
import wiki.zimo.certificate_manage.dao.TeacherDao;
import wiki.zimo.certificate_manage.domain.Major;
import wiki.zimo.certificate_manage.domain.School;
import wiki.zimo.certificate_manage.domain.Teacher;
import wiki.zimo.certificate_manage.domain.User;
import wiki.zimo.certificate_manage.service.ExcelService;
import wiki.zimo.certificate_manage.utils.UUIDUtils;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 *
 * @author 子墨
 * @date 2018/10/23 15:06
 * @describe 处理教师
 */
@Controller
@RequestMapping("/teacher")
public class TeacherController {

    @Autowired
    private TeacherDao teacherDao;
    @Autowired
    private MajorDao majorDao;
    @Autowired
    private SchoolDao schoolDao;
    @Autowired
    private ExcelService excelService;

    /**
     * 新增页
     * @author 子墨
     * @date 2018/10/19 10:37
     * @params []
     * @return java.lang.String
     */
    @RequestMapping(value = "add")
    public String add(Map<String,Object> map){
        Sort sort = new Sort(Sort.Direction.ASC, "majorName");
        List<Major> majors = majorDao.findAll(sort);
        List<School> schools = schoolDao.findAll();
        map.put("majors",majors);
        map.put("schools",schools);
        return "teacher/add";
    }

    /**
     * 列表页
     * @author 子墨
     * @date 2018/10/19 10:38
     * @params []
     * @return java.lang.String
     */
    @RequestMapping(value = "list")
    public String list(){
        return "teacher/list";
    }

    /**
     * 更新页
     * @author 子墨
     * @date 2018/10/19 15:03
     * @params [map, id]
     * @return java.lang.String
     */
    @RequestMapping(value = "update")
    public String update(Map<String,Object> map,
                         @RequestParam(value = "id",required = true)String id){
        Teacher teacher = teacherDao.findTeacherById(id);
        map.put("teacher",teacher);
        return add(map);
    }

    /**
     * 一键导出Excel数据
     * @author 子墨
     * @date 2018/10/19 15:09
     * @params [response]
     * @return void
     */
    @RequestMapping(value = "/export")
    public void exportExcel(HttpServletResponse response,HttpSession session) {
        User user = (User) session.getAttribute(Constant.USER);
        List<Teacher> list = new ArrayList<>();
        if (user == null){
            return;
        }
        String role = user.getRole();
        switch (role){
            case Constant.ROLE_TEACHER:
                list.add(user.getTeacher());
                break;
            case Constant.ROLE_LEADER:
                list = teacherDao.findAll();
                break;
        }
        System.out.println(list.toString());
        // 指定列表标题和工作表名称
        ExportParams params = new ExportParams("辅导员信息","辅导员信息表");
        try {
            excelService.doExportExcel(response,list,Teacher.class,params,"辅导员信息表");
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 新增或者修改辅导员信息接口
     * @author 子墨
     * @date 2018/10/18 22:53
     * @params [map, id, schoolName]
     * @return wiki.zimo.certificate_manage.domain.School
     */
    @RequestMapping(value = "/addTeacher")
    @ResponseBody
    public Teacher addTeacher(Map<String,Object> map,
                            @RequestParam(value = "id",required = false)String id,
                            @RequestParam(value = "teacherNum",required = true)String teacherNum,
                            @RequestParam(value = "teacherPassword",required = false,defaultValue = "123456")String teacherPassword,
                            @RequestParam(value = "teacherName",required = true)String teacherName,
                            @RequestParam(value = "teacherTel",required = true)String teacherTel,
                            @RequestParam(value = "teacherEmail",required = false)String teacherEmail,
                            @RequestParam(value = "teacherClass",required = true)String teacherClass,
                            @RequestParam(value = "teacherSchool",required = true) String teacherSchool){
        if (id == null || id.replaceAll(" ","").equals("")){
            Teacher teacher = new Teacher();
            teacher.setId(UUIDUtils.getRandomID());
            teacher.setTeacherNum(teacherNum);
            teacher.setTeacherPassword(teacherPassword);
            if (!teacherPassword.replaceAll(" ","").equals("")){
                teacher.setTeacherPassword(teacherPassword);
            }else {
                teacher.setTeacherPassword(Constant.DEFAULT_PASSWORD);
            }
            teacher.setTeacherName(teacherName);
            teacher.setTeacherTellphone(teacherTel);
            teacher.setTeacherEmail(teacherEmail);
            teacher.setTeacherClass(teacherClass);
            teacher.setTeacherSchool(teacherSchool);
            return teacherDao.save(teacher);
        }
        Teacher teacher = teacherDao.findTeacherById(id);
        teacher.setTeacherNum(teacherNum);
        if (!teacherPassword.replaceAll(" ","").equals("")){
            teacher.setTeacherPassword(teacherPassword);
        }else {
            teacher.setTeacherPassword(Constant.DEFAULT_PASSWORD);
        }
        teacher.setTeacherName(teacherName);
        teacher.setTeacherTellphone(teacherTel);
        teacher.setTeacherEmail(teacherEmail);
        teacher.setTeacherClass(teacherClass);
        teacher.setTeacherSchool(teacherSchool);
        return teacherDao.save(teacher);
    }

    /**
     * 得到所有辅导员信息接口
     * @author 子墨
     * @date 2018/10/19 12:38
     * @params []
     * @return java.util.List<wiki.zimo.certificate_manage.domain.Teacher>
     */
    @RequestMapping(value = "/getAllTeacher")
    @ResponseBody
    public List<Teacher> getAllTeacher(HttpSession session){
        User user = (User) session.getAttribute(Constant.USER);
        if (user == null){
            return null;
        }
        String role = user.getRole();
        switch (role){
            case Constant.ROLE_TEACHER:
                List<Teacher> teachers = new ArrayList<>();
                teachers.add(teacherDao.findTeacherById(user.getTeacher().getId()));
                return teachers;
            case Constant.ROLE_STUDENT:
                return null;
        }
        return teacherDao.findAll();
    }

    /**
     * 删除辅导员信息接口
     * @author 子墨
     * @date 2018/10/19 14:11
     * @params [id]
     * @return java.lang.String
     */
    @RequestMapping(value = "delete")
    public String delete(Map<String,Object> map,
                         @RequestParam(value = "id")String id){
        if (teacherDao.existsById(id)){
            teacherDao.deleteById(id);
        }
        map.put(Constant.MSG,"提示：删除成功");
        return list();
    }
}
